package com.jsgt.greekfire.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.AppContext
import com.fhx.core.base.BaseViewModel
import com.jsgt.greekfire.common.util.MD5Util
import com.fhx.core.utils.SystemUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.bean.*
import com.jsgt.greekfire.common.http.ResponseData
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.dao.model.MarketBean
import com.jsgt.greekfire.repository.CoinRepository
import com.jsgt.greekfire.ui.mine.sermoncommunity.Community


/**
 * author fhx
 * create time 2020/4/25 18:23
 **/
class CoinViewModel() : BaseViewModel() {
    private val coinRepository = CoinRepository.instance
    val errorLiveData by lazy { MutableLiveData<Any>() }
    val marketListLiveData by lazy { MutableLiveData<List<MarketBean>>() }
    val searchListLiveData by lazy { MutableLiveData<List<MarketBean>>() }
    val myMarketListLiveData by lazy { MutableLiveData<List<MarketBean>>() }
    val marketInfoListLiveData by lazy { MutableLiveData<CoinBean>() }
    val marketInfoByName by lazy { MutableLiveData<CoinBean>() }
    val selfChooseLiveData by lazy { MutableLiveData<CoinBean>() }
    val mySelfMarketInfoLiveData by lazy { MutableLiveData<CoinBean>() }
    val deleteMySelfChoose by lazy { MutableLiveData<CoinBean>() }
    val snapUpListLiveData by lazy { MutableLiveData<ResponseData<List<SnapUpBean>>>() }
    val saleListLiveData by lazy { MutableLiveData<List<CoinBean>>() }
    val snapUpLiveData by lazy { MutableLiveData<Any>() }
    val snapUpErrorLiveData by lazy { MutableLiveData<String>() }
    val rechargeAddressLiveData by lazy { MutableLiveData<RechargeAddressBean>() }
    val myRechargeAddressLiveData by lazy { MutableLiveData<RechargeAddress>() }
    val rollInOrOutLiveData by lazy { MutableLiveData<Any>() }
    val sellCoinLiveData by lazy { MutableLiveData<Any>() }
    val buyCoinLiveData by lazy { MutableLiveData<Any>() }
    val assetUpdateLiveData by lazy { MutableLiveData<Any>() }
    val withdrawCoinLiveData by lazy { MutableLiveData<Any>() }
    val transferLiveData by lazy { MutableLiveData<Any>() }
    val exchangeLiveData by lazy { MutableLiveData<Any>() }
    val tradingRedSaleLiveData by lazy { MutableLiveData<List<CoinBean>>() }
    val delegationBeanListLiveData by lazy { MutableLiveData<List<CoinBean>>() }
    val delegationHistoryLiveData by lazy { MutableLiveData<List<CoinBean>>() }
    val tradingGreenSaleLiveData by lazy { MutableLiveData<List<CoinBean>>() }
    val managerChargeLiveData by lazy { MutableLiveData<List<ManageBean>>() }
    val cancelOrderLiveData by lazy { MutableLiveData<Any>() }
    val incomeRecordLiveData by lazy { MutableLiveData<List<IncomeRecordBean>>() }
    val snapUpNumLiveData by lazy { MutableLiveData<Double>() }

    //val ustdPriceLiveData = MutableLiveData<PriceBean>()
    val genesisBlockStatusLiveData by lazy { MutableLiveData<GenesisBlockStatusBean>() }
    val joinGenesisBlockLiveData by lazy { MutableLiveData<Any>() }
    val checkGenesisBlockStatusLiveData by lazy { MutableLiveData<BlockStatus>() }
    val checkGenesisBlockparticipateLiveData by lazy { MutableLiveData<Any>() }

    val myCommunityLiveData by lazy { MutableLiveData<Community>() }
    val convertLiveData by lazy { MutableLiveData<Any>() }
    val convertConfigData by lazy { MutableLiveData<ResponseData<List<ConvertConfigBean>>>() }
    val convertCoinData by lazy { MutableLiveData<Any>() }

    /**
     * 行情列表
     */
    fun getMarketList(sortName: String, sort: Boolean) {
        val params = HashMap<String, Any>()
        params["sortName"] = sortName
        params["sort"] = sort
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMarketList()
                if (data.code == 0) {
                    marketListLiveData.value = data.data
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 行情列表
     */
    fun getMarketList() {
//        val params = HashMap<String, Any>()
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMarketList()
                if (data.code == 0) {
                    marketListLiveData.value = data.data
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 搜索币种
     */
    fun searchMarketList(name: String) {
        val params = HashMap<String, Any>()
        params["name"] = name
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMarketList()
                if (data.code == 0) {
                    searchListLiveData.value = data.data
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 我的行情列表
     */
    fun getMyMarketList(sortName: String, sort: Boolean) {
        val params = HashMap<String, Any>()
        params["sortName"] = sortName
        params["sort"] = sort
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMyMarketList(params)
                if (data.code == 0) {
//                    myMarketListLiveData.value = data.result
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 行情详情
     */
    fun getMarketInfo(guid: String) {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMarketInfo(guid)
                if (data.code == 0) {
                    marketInfoListLiveData.value = data.data
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 通过币名称获取行情详情
     */
    fun getMarketInfoByName() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMarketInfoByName()
                if (data.code == 0) {
                    marketInfoByName.value = data.utiMarket
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 添加自选
     */
    fun addSelfMarket(marketGuid: String) {
        val params = HashMap<String, Any>()
        params["marketGuid"] = marketGuid
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.addSelfMarket(params)
                if (data.code == 0) {
                    selfChooseLiveData.value = data.data
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 删除我的自选
     */
    fun deleteMyMarket(marketGuid: String) {
        val params = HashMap<String, Any>()
        params["marketGuid"] = marketGuid
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.deleteMyMarket(params)
                if (data.code == 0) {
                    deleteMySelfChoose.value = data.data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 行情自选状态
     */
    fun getMySelfMarketInfo(marketGuid: String) {
        val params = HashMap<String, Any>()
        params["marketGuid"] = marketGuid
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getMySelfMarketInfo(params)
                if (data.code == 0) {
                    mySelfMarketInfoLiveData.value = data.result
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 抢购
     */
    fun snappedUpCoin(configId: String, amount: String, tradingPwd: String) {
        val params = HashMap<String, Any>()
        params["configId"] = configId
        params["amount"] = amount
        params["payPassword"] = MD5Util.getMD5(tradingPwd)
        params["device"] = SystemUtil.getDeviceUUID(AppContext.instance)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.snappedUpCoin(params)
                if (data.code == 0) {
                    snapUpLiveData.value = data.data
                } else {
                    snapUpErrorLiveData.value = data.msg
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 抢购列表
     */
    fun snappedUpCoinList() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.snappedUpCoinList()
                if (data.code == 0) {
                    snapUpListLiveData.value = data
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 交易列表
     */
    fun saleCoinList() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.saleCoinList()
                if (data.code == 0) {
                    saleListLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 充币地址
     */
    fun rechargeAddress(assetGuid: String) {
        val params = HashMap<String, Any>()
        params["assetGuid"] = assetGuid
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.rechargeAddress(params)
                if (data.code == 0) {
                    rechargeAddressLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 个人充币地址
     */
    fun getRechargeAddress() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getRechargeAddress()
                if (data.code == 0) {
                    myRechargeAddressLiveData.value = data.data
                } else {
                    errorLiveData.value = data.msg
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 转入转出
     */
    fun rollInOrOut(status: String, amount: String, assetId: String) {
        val params = HashMap<String, Any>()
        params["status"] = status
        params["amount"] = amount
        params["assetId"] = assetId
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.rollInOrOut(params)
                if (data.code == 0) {
                    rollInOrOutLiveData.value = data.msg
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 卖币
     */
    fun sellCoin(
        totalAmt: String, assetGuid: String, assetPrice: String,
        payPassword: String, adType: String
    ) {
        val params = HashMap<String, Any>()
        params["totalAmt"] = totalAmt
        params["assetGuid"] = assetGuid
        params["assetPrice"] = assetPrice
        params["payPassword"] = MD5Util.getMD5(payPassword)
        params["adType"] = adType
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.sellCoin(params)
                if (data.code == 0) {
                    sellCoinLiveData.value = data.msg
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 买币
     */
    fun buyCoin(
        sellerGuid: String,
        adGuid: String,
        paymentAssetId: String,
        adPrice: String,
        adAmt: String,
        incomeAssetId: String,
        adFee: String
    ) {
        val params = HashMap<String, Any>()
        params["adMoney"] = adGuid
        params["adPrice"] = adPrice
        params["paymentAssetId"] = paymentAssetId
        params["sellerGuid"] = sellerGuid
        params["adAmt"] = adAmt
        params["incomeAssetId"] = incomeAssetId
        params["adFee"] = adFee
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.buyCoin(params)
                if (data.code == 0) {
                    buyCoinLiveData.value = data.msg
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 资产管理更新
     */
    fun userAssetUpdate(guid: String, display: String) {
        val params = HashMap<String, Any>()
        params["guid"] = guid
        params["display"] = display
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.userAssetUpdate(params)
                if (data.code == 0) {
                    assetUpdateLiveData.value = data.msg
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 提币
     */
    fun withdrawCoin(
        type: Int,
        address: String,
        remarks: String,
        assetTypeId: String,
        money: String,
        payPassword: String
    ) {
        val params = HashMap<String, Any>()
        params["type"] = type.toString()
        params["address"] = address
        params["status"] = "1"
        params["serviceFee"] = "0.01"
        params["remarks"] = remarks
        params["assetTypeId"] = assetTypeId
        params["money"] = money
        params["payPassword"] = MD5Util.getMD5(payPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.withdrawCoin(params)
                if (data.code == 0) {
                    withdrawCoinLiveData.value = data.msg
                } else {
                    ToastUtils.show(data.msg!!)
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 转账
     */
    fun transferToAddress(
        amount: String,
        assetId: String,
        addr: String,
        payPassword: String
    ) {
        val params = HashMap<String, Any>()
        params["amount"] = amount
        params["assetId"] = assetId
        params["addr"] = addr
        params["payPassword"] = MD5Util.getMD5(payPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.transferToAddress(params)

                if (data.code == 0) {
                    transferLiveData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 管理费列表
     */
    fun getManagerCharge(guid: String) {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getManagerCharge(guid)
                if (data.code == 0) {
                    managerChargeLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 划转
     */
    fun exchange(
        ustdAmount: String,
        glbAmount: String,
        payPassword: String
    ) {
        val params = HashMap<String, Any>()
        params["ustdAmount"] = ustdAmount
        params["glbAmount"] = glbAmount
        params["payPassword"] = MD5Util.getMD5(payPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.exchange(params)
                if (data.code == 0) {
                    exchangeLiveData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * usdt兑换GFC
     */
    fun u2GFC(
        ustdAmount: String,
        gfcAmount: String,
        tradingPassword: String
    ) {
        val params = HashMap<String, Any>()
        params["ustdAmount"] = ustdAmount
        params["gfcAmount"] = gfcAmount
        params["tradingPassword"] = MD5Util.getMD5(tradingPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.u2GFC(params)
                if (data.code == 0) {
                    exchangeLiveData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 交易涨列表
     */
    fun saleRedList(adType: String) {
        val params = HashMap<String, Any>()
        params["adType"] = adType
        params["adStatus"] = "0"
        params["deleteFlag"] = "0"
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.saleList(params)
                if (data.code == 0) {
                    tradingRedSaleLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 交易跌列表
     */
    fun saleGreenList(adType: String) {
        val params = HashMap<String, Any>()
        params["adType"] = adType
        params["adStatus"] = "0"
        params["deleteFlag"] = "0"
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.saleList(params)
                if (data.code == 0) {
                    tradingGreenSaleLiveData.value = data.list
                } else {
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 首页当前委托列表
     */
    fun currentSaleList() {
        val params = HashMap<String, Any>()
        params["adStatus"] = "0"
        params["deleteFlag"] = "0"
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.saleList(params)
                if (data.code == 0) {
                    delegationBeanListLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 历史委托列表
     */
    fun historySaleList() {
        val params = HashMap<String, Any>()
        params["adStatus"] = "1"
        params["deleteFlag"] = "0"
        params["adType"] = ""
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.saleList(params)
                if (data.code == 0) {
                    delegationHistoryLiveData.value = data.list
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 取消挂单
     */
    fun cancelOrder(guid: String) {
        val params = HashMap<String, Any>()
        params["guid"] = guid
        params["adStatus"] = "1"
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.cancelOrder(params)
                if (data.code == 0) {
                    cancelOrderLiveData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 获取USTD换算比例
     */
    /*fun getUSTDPrice() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getUSTDPrice()
                if (data.code == 0) {
                    ustdPriceLiveData.value = data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }*/

    fun getIncomeRecord(page: Int, size: Int) {
        val params = HashMap<String, Any>()
        params["page"] = page.toString()
        params["limit"] = size.toString()
        params["order"] = "desc" //排列顺序 asc/desc
        params["sidx"] = "create_time"  //按sidx字段排列
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.getIncomeList(params)
                if (data.code == 0) {
                    incomeRecordLiveData.value = data.page?.list
                } else {
                    incomeRecordLiveData.value = null
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 抢购限制
     */
    fun residueAmount() {
        val params = HashMap<String, Any>()
        params["device"] = SystemUtil.getDeviceUUID(AppContext.instance)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.residueAmount(params)
                if (data.code == 0) {
                    snapUpNumLiveData.value = data.result
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 检查创世区块的状态
     */
    fun checkGenesisBlock() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.checkGenesisBlock()
                if (data.code == 0) {
                    genesisBlockStatusLiveData.value = data.result
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 创世区块配置中的状态
     */
    fun checkGenesisBlockStatus() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.checkGenesisBlockStatus()
                if (data.code == 0) {
                    checkGenesisBlockStatusLiveData.value = data.data
                } else {
                    errorLiveData.value = data

                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 用户参与 创世区块 抢购
     */
    fun checkGenesisBlockparticipate(gfcAmount: Double, genesisGuid: Int, tradingPassword: String) {

        val params = HashMap<String, Any>()
        params["gfcAmount"] = gfcAmount
        params["genesisGuid"] = genesisGuid
        params["tradingPassword"] = MD5Util.getMD5(tradingPassword)

        Log.e("TAG", "checkGenesisBlockparticipate: " + params)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.checkGenesisBlockparticipate(params)
                if (data.code == 0) {
                    checkGenesisBlockparticipateLiveData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 加入创世区块
     */
    fun joinGenesisBlock() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.joinGenesisBlock()
                if (data.code == 0) {
                    joinGenesisBlockLiveData.value = data.msg
                } else {
                    errorLiveData.value = data.msg
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 布道社区
     * api/userperformance/myCommunity
     */

    fun myCommunity() {
        val params = HashMap<String, Any>()
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.myCommunity(params)
                if (data.code == 0) {
                    myCommunityLiveData.value = data.data
                    Log.e("TAG", "myCommunity: " + data.data.toString())

                } else {
                    errorLiveData.value = data.msg
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }

    }


    /**
     * 用户闪兑接口
     * http://ip:8081/greekfire/api/gfcuserasset/convert
     */

    fun convert(gfcAmount: String, gftAmount: String, tradingPassword: String) {
        val params = HashMap<String, Any>()
        params["gfcAmount"] = gftAmount
        params["gftAmount"] = ""
        params["tradingPassword"] = tradingPassword
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.convert(params)
                if (data.code == 0) {
                    convertLiveData.value = data.code
                    ToastUtils.show("闪兑成功")

                } else {
                    errorLiveData.value = data.msg
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }

    }

    /**
     * 转入
     * http://ip:8081/greekfire/api/gfcuserasset/convert
     */

    fun poolIn(assetGuid: Int, assetAmount: String, tradingPassword: String) {
        val params = HashMap<String, Any>()
        params["assetGuid"] = assetGuid
        params["assetAmount"] = assetAmount
        params["tradingPassword"] = tradingPassword
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.poolIn(params)
                if (data.code == 0) {
                    convertLiveData.value = data.code
                    ToastUtils.show("转入成功")
                } else {
                    errorLiveData.value = data.msg
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }

    }

    /**
     * 转出
     * http://ip:8081/greekfire/api/gfcuserasset/convert
     */

    fun poolOut(assetAmount: String, tradingPassword: String) {
        val params = HashMap<String, Any>()
        params["assetAmount"] = assetAmount
        params["tradingPassword"] = tradingPassword
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.poolOut(params)
                if (data.code == 0) {
                    convertLiveData.value = data.code
                    ToastUtils.show("转出成功")

                } else {
                    errorLiveData.value = data.msg
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 兑换配置列表
     */
    fun convertConfigList() {
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.convertConfigList()
                if (data.code == 0) {
                    convertConfigData.value = data
                } else {
                    errorLiveData.value = data.msg
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 兑换
     */
    fun convertCoin(
        fromAssetId: String,
        toAssetId: String,
        fromCoin: String,
        toCoin: String,
        number: String,
        tradingPassword: String
    ) {
        val params = HashMap<String, Any>()
        params["fromAssetId"] = fromAssetId
        params["toAssetId"] = toAssetId
        params["fromCoin"] = fromCoin
        params["toCoin"] = toCoin
        params["number"] = number
        params["tradingPassword"] = MD5Util.getMD5(tradingPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = coinRepository.convertCoin(params)
                if (data.code == 0) {
                    convertCoinData.value = data.msg
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


}